﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Microsoft.Practices.EnterpriseLibrary.Data;
namespace DuJianliang.ApplicationPlatform.TempEquipment
{
    public partial class TempEquipmentDetailEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ParentID"] != null)
                {
                    ViewState["ParentID"] = Request.QueryString["ParentID"].Trim();

                    if (Request.QueryString["ID"] != null)
                    {
                        ViewState["ID"] = Request.QueryString["ID"].Trim();

                        var dal = new TempEquipmentDal();

                        Model.TempEquipmentDetialOB obj = dal.getTempEquipmentDetial(ViewState["ID"].ToString());

                        lblItemCode.Text = obj.equipmentName;
                        hidItemCode.Value = obj.equipmentName; 
                        lbl_Model.Text = obj.Model;
                        hidItemName.Value = obj.Model;
                        lbl_Unit.Text = obj.unit;
                        hidItemUnit.Value = obj.unit;
                        txt_reMark.Text = obj.reMark;                     
                        txt_Amount.Text = obj.amount;
                    }
                }                
            }
        }

        private DataTable GetItemCodeTable()
        {
            Database db = DatabaseFactory.CreateDatabase(Common.Helper.Instance.ConnectionStringName_SBODB);

            string sql = "select ItemCode,ItemCode + '(' + ItemName + ')' AS ItemName2, ItemName, InvntryUom from OITM WHERE OITM.itmsgrpcod<>157";

            DataTable result = db.ExecuteDataSet(CommandType.Text, sql).Tables[0];
            return result;
        }


        protected void btnOK_Click(object sender, EventArgs e)
        {

            string equipmentName = hidItemCode.Value; // lblItemCode.Text.Trim();
            string Model = hidItemName.Value; // lbl_Model.Text.Trim();
            string unit = hidItemUnit.Value; // lbl_Unit.Text.Trim();
            string amount = txt_Amount.Text.Trim();
            string reMark = txt_reMark.Text.Trim();


            Model.TempEquipmentDetialOB obj = new Model.TempEquipmentDetialOB();

            obj.ParentID = ViewState["ParentID"].ToString();
            obj.equipmentName = equipmentName;
            obj.Model = Model;
            obj.unit = unit;
            obj.amount = amount;
            obj.reMark = reMark;

            var dal = new TempEquipmentDal();
            if (ViewState["ID"] != null)
            {
                obj.ID = ViewState["ID"].ToString();
                dal.TempEquipmentDetial_Update(obj);
            }
            else
            {
                obj.ID = Guid.NewGuid().ToString();
                dal.TempEquipmentDetial_Insert(obj);
            }

            Response.Write("<script>window.opener = null;window.open('', '_self', '');window.close();</script>");
            
        }

        protected void btn_Back_Click(object sender, EventArgs e)
        {
            Response.Write("<script>window.opener = null;window.open('', '_self', '');window.close();</script>");
        }
    }
}